import {observable, action} from 'mobx';
import { ListStore } from './listStore'
import { WeaTools } from 'ecCom';
import * as API_LIST from '../apis/fnaAdvancePayment'
import * as API_HIS from '../apis/rptHistory'
import { WeaForm } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 预付款查询表的Store
 */
export class FnaAdvancePaymentStore extends ListStore{
	constructor(props){
        super(props);
    }

    @observable hasRight=1;
    @observable loading;

    //GUID
    guid = '';

    //是否点击了高级查询
    @observable isSearch = false;

    //保存模型的数据
    modalSaveParam = {
        tbName: ''
    };

    //保存报表的Modal的显隐
    @observable showModal = false;

    initDatas = (params = {})=>{
		this.initInfo("init");
		let value = Object.assign({},{
				qryFunctionType:0,
				rptViewType:0
			});

		this.doSearch(value);
	}

	//初始化高级搜索
	initInfo = (value,params = {}) =>{
		API_LIST.fnaAdvancePaymentInfoP(params).then(data => {
            if(data.flag == 'success'){
                this.hasRight = 1;

                const { conditions } = data;
                this.condition = conditions;
                this.form = new WeaForm();
                this.form.initFormFields(conditions);
                //初始化tab列表  
                let tabTitle = [{title:getLabel(24616,'查询报表'),key:0}];
                this.topTab = tabTitle;
                this.setShowSearchAd(true);
            } else {
                this.hasRight = 4;
            }
		});	
    }
    
    //生成GUID
    generGUID=(func)=>{
        API_HIS.getGUIDP().then(data => {
            this.guid = data.guid;
            func && func(); //外部的回调,防止程序速度，快与XHR
        });
    }

    //保存GUID
    saveGUID=(value)=>{
        this.guid = value;
    }

	//保存临时数据
	saveTempData=(params = {},callback)=>{
		const searchParamsAd = this.form.getFormParams();
		const newParams = {  ...searchParamsAd, ...params };

		API_LIST.saveFnaAdvancePaymentTempData(newParams).then(data => {
            let result = JSON.parse(data.resultJson)
            if(result.flag){
                callback && callback();
            } else {
                message.error(result.msg);
                this.isSearch = false;
            }
		});
	}    

	//TABLE数据
	doSearch =(params = {},flag = true)=> {
        const searchParamsAd = this.form.getFormParams();
        // console.log('searchParamsAd', searchParamsAd)
        const newParams = {  ...searchParamsAd, ...params };
        this.loading = true; 
        API_LIST.fnaAdvancePaymentReportP(newParams).then(data => {
            if(data.flag == 'success'){
                this.hasRight = 1;
                if(flag){//回到第一页
                    this.tableStore.getDatas(data.datas,1);
                }else{
                    this.tableStore.getDatas(data.datas);
                }
                this.dataKey = data.datas;
                this.loading = false;
            } else {
                this.hasRight = 4;
            }  
            this.loading = false;           
        });
    }

    //保存报表
    saveModelInfo=(params)=>{
        this.loading = true;
    	API_HIS.saveReportP(params).then(data=>{
    		if(data.flag == 'success'){
                this.hasRight = 1; 
            }else{
                this.hasRight = 4;
            }
            this.loading = false;
    	});
    }    

    //是否点击了高级搜索
    setIsSearch = (bool)=>{
    	this.isSearch = bool;
    }
    
    //设置高级搜索的显示
    setShowSearchAd = (value) => {
        this.showSearchAd = value;
    }

    //设置路由
    setRouter = (value) =>{
        this.router = value;
    }    

    //数据重置，防止存留数据导致的页面显示不正常
    resetData = () => {

        //tab默认值
        this.searchParams = {
            index:0
        };

        //GUID
        this.guid='';

        //是否点击了高级查询
        this.isSearch = false;

    }    

}